      SUBROUTINE ROTSLIP1(KCOORD,TVEC,DELEPOCH,ROTMTX,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C GIVEN :  1. WE HAVE A TIME AND SPACECRAFT STATE VECTOR IN MEAN OF
C             1950.0, MEAN OF DATE, OR TRUE OF DATE THAT HAS BEEN
C             COMPUTED FROM SPECIFIC SET OF INITIAL CONDITIONS. (A
C             COLLECTION OF THESE IS A SPACECRAFT EPHEMERIS)
C
C          2. WE WANT TO USE A DIFFERENT EPOCH AND INITIAL CONDITIONS
C             BUT WE WANT THE EARTH-FIXED TRAJECTORY PRESERVED. THAT IS,
C             THE SPACECRAFT LATITUDE, LONGITUDE, AND RADIUS DEPEND ON
C             THE TIME FROM EPOCH AND ARE INDEPENDENT OF THE EPOCH
C             CHOSEN. THIS IS THE CASE WHEN A LAUNCH SLIP OCCURS. 
C
C          3. WE WANT TO USE THE KNOWN SPACECRAFT STATE VECTOR TO
C             DETERMINE A NEW STATE VECTOR REFLECTING THE EPOCH CHANGE.
C             WE WANT THE NEW VECTOR IN THE SAME COORDINATE SYSTEM AS
C             THE OLD.
C
C FIND :   THE ROTATION MATRIX THAT CONVERTS THE OLD STATE VECTOR TO
C          THE NEW STATE VECTOR.
C
C
C USAGE NOTES:
C
C    1. SINCE THE MEAN OF DATE AND TRUE OF DATE SYSTEMS ARE PRECESSING,
C       ALTHOUGH SLOWLY, STATE TRANSFORMATIONS ARE OF THE FORM
C
C          POSNEW = ROTMTX * POSOLD
C          VELNEW = ROTMTX * VELOLD + ROTMTXDOT * POSOLD.
C
C       FOR MANY APPLICATIONS, ROTMTXDOT SMALL ENOUGH SO ITS TERM MAY
C       BE IGNORED FOR VELOCITY USE.
C
C       REFERENCE IS METHODS OF ORBIT DETERMINATION, P. R. ESCOBAL,
C       1983 REPRINT WITH CORRECTIONS OF 1965 EDITION, PAGES 126FF
C
C    2. THIS METHOD OF ROTATION IS VALID IF THE FORCES ACTING ON THE
C       SPACECRAFT ARE EARTH-FIXED. ZONAL AND TESSERAL FORCES ARE
C       UNCHANGED BY THE CHANGE IN EPOCH. SUN AND MOON GRAVITATION
C       FORCES ARE CHANGED BY THE CHANGE IN EPOCH, SO A TRAJECTORY
C       GENERATED WITH THEM DOES NOT FOLLOW THE SAME EARTH-FIXED
C       TRAJECTORY FOR DIFFERENT EPOCHS. LIKEWISE, IF A SUN-DEPENDENT
C       DRAG MODEL IS USED, THE EARTH-FIXED TRAJECTORY WILL VARY.
C
C       THEREFORE, THIS METHOD IS NOT VALID WHEN THE SUN, MOON, AND
C       SUN-DEPENDENT DRAG FORCES ARE USED. IT IS VALID WHEN THE
C       EARTH ZONALS AND TESSERALS ARE USED.
C
C
C VARIABLES DIM TYPE I/O DESCRIPTION
C --------- --- ---- --- -----------
C
C KCOORD     1   I*4  I  A FLAG INDICATING THE COORDINATE SYSTEM IN
C                        WHICH THE OLD AND NEW VECTORS ARE GIVEN.
C
C                         = 1, GEOCENTRIC MEAN EQTR/EQNX OF 1950.0
C                         = 2, GEOCENTRIC MEAN EQTR/EQNX OF DATE
C                         = 3, GEOCENTRIC TRUE EQTR/EQNX OF DATE
C                         = OTHERWISE, ERROR.
C
C TVEC       1   I*4  I  THE TIME ASSOCIATED WITH THE OLD VECTOR.
C                        IN SECONDS SINCE 1/1/50, 0.0 HRS.
C
C                        NEEDED FOR KCOORD = 1 AND KCOORD = 2,
C                        IGNORED FOR KCOORD = 3.
C
C DELEPOCH   1   R*8  I  THE EPOCH DIFFERENCE, NEW EPOCH - OLD EPOCH.
C                        IN SECONDS. THERE IS NO LIMIT ON THE SIZE
C                        OF THE DIFFERENCE.
C
C ROTMTX    3,3  R*8  O  THE ROTATION MATRIX RELATING THE OLD AND
C                        NEW VECTORS.
C
C                        IF VECOLD = OLD VECTOR, VECNEW = NEW VECTOR
C                        THEN:
C                           VECNEW(I) = ROTMTX(I,J)* VECOLD(J), SUM ON J
C
C LUERR      1   I*4  I  THE FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                        IF ZERO OR NEGATIVE, NO MESSAGES ARE GIVEN.
C
C IERR       1   I*4  O  ERROR RETURN FLAG.
C                        = 0, NO ERROR.
C                        = 1, ERROR. ONLY ERROR IS BAD KCOORD VALUE.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742. 5/85.
C        MODIFIED....
C
C***********************************************************************
C
C
      REAL*8 DEGRAD / 57.29577951308232D0 /
      REAL*8 ROTMTX(3,3),ROT1(3,3),ROT2(3,3),ROT3(3,3)
      REAL*8 AIDENT(3,3)/
     *   1.D0,0.D0,0.D0,  0.D0,1.D0,0.D0,  0.D0,0.D0,1.D0/
      LOGICAL NEEDROT1,NEEDROT2,NEEDROT3
      LOGICAL OKROT1/.FALSE./,OKROT2/.FALSE./,OKROT3/.FALSE./
      INTEGER KCOORDL/-99/
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) WRITE(LUBUG,8501) KCOORD,PAKTIM50(TVEC),
     *    DELEPOCH/86400.D0
C
C INITIALIZATION FOR THIS CALL
C
      IERR = 0
C
      NEEDROT2 = DELEPOCH.NE.0.D0
      NEEDROT1 = KCOORD.NE.3 .AND. NEEDROT2
      NEEDROT3 = KCOORD.NE.3 .AND. NEEDROT2
C
      OKROT1 = OKROT1 .AND.
     *             KCOORD.EQ.KCOORDL .AND. TVEC.EQ.TROT1
      OKROT2 = OKROT2 .AND.
     *             DELEPOCH.EQ.DTROT2
      OKROT3 = OKROT3 .AND.
     *             KCOORD.EQ.KCOORDL .AND. (TVEC+DELEPOCH).EQ.TROT3
C
      IF(IBUG.NE.0) WRITE(LUBUG,8502)
     *  NEEDROT1,NEEDROT2,NEEDROT3,  OKROT1,OKROT2,OKROT3
C
C
C ERROR CHECK.
C
      IF(KCOORD.LT.1 .OR.KCOORD.GT.3) THEN
        IERR = 1
        CALL MTXSETR8(ROTMTX,0.D0,3,3)
        IF(LUERR.GT.0) WRITE(LUERR,1001) KCOORD
 1001   FORMAT(/,' ROTSLIP ERROR. BAD COORDINATE SYS FLAG. VALUE=',I4/,
     *           '                ERROR RETURN WITH ROTMTX = ZEROS')
C      RESET OKROT'S FOR NEXT CALL TO ENSURE PROPER LOADING OF ROT'S.
        OKROT1 = .FALSE.
        OKROT2 = .FALSE.
        OKROT3 = .FALSE.
        GO TO 9999
        END IF
C
C
C *** METHOD ***
C
C  WE WANT TO COMPUTE THE MATRIX THAT ROTATES POSITION AND VELOCITY
C  VECTORS SO THAT THE GROUND TRACK IS UNCHANGED BY A CHANGE IN EPOCH.
C  SINCE THE EARTH IS ROTATING WITHIN A TRUE OF DATE COORDINATE SYSTEM,
C  TO PRESERVE THE GROUND TRACK, WE MUST:
C
C        1. COMPUTE ROT1, THE MATRIX THAT ROTATES VECTORS FROM THE
C           GIVEN SYSTEM TO THE TRUE OF DATE SYSTEM, WHERE 'DATE' IS
C           TIME TVEC.
C        2. COMPUTE ROT2, THE MATRIX THAT ROTATES VECTORS WITHIN THE
C           TRUE OF DATE SYSTEM. THE ROTATION IS ABOUT THE EARTH Z-AXIS
C           BY THE SAME ANGLE THROUGH WHICH THE EARTH ROTATES IN THE
C           TIME SPAN COVERED BY THE EPOCH DIFFERENCE. ROT2 IS THE
C           MATRIX THAT PRESERVES THE EARTH-FIXED TRAJECTORY.
C        3. COMPUTE ROT3, THEN MATRIX THAT ROTATES VECTORS FROM TRUE OF
C           DATE SYSTEM BACK TO THE GIVEN SYSTEM. HERE, 'DATE' IS
C           TIME ASSOCIATED WITH THE NEW VECTOR. THAT IS, 'DATE' IS
C           TVEC + DELEPOCH.
C
C
C
C GET ROT1, THE MATRIX THAT ROTATES FROM THE GIVEN COORDINATE SYSTEM TO
C THE TRUE OF DATE COORDINATE SYSTEM.
C
      IF(NEEDROT1) THEN
        IF(.NOT.OKROT1) THEN
          IF(KCOORD.EQ.1) THEN                     ! M50 -> TOD
            CALL M50TOD(TVEC,1,ROT1)
          ELSE                                     ! MDT -> TOD
            CALL MDTTOD(TVEC,1,ROT1)
            END IF
          TROT1 = TVEC
          OKROT1 = .TRUE.
          IF(IBUG.NE.0) WRITE(LUBUG,8503) 'ROT1 COMPUTED.'
          END IF
        END IF
C
C
C GET ROT2, THE MATRIX THAT ROTATES THE TRUE OF DATE STATE TO ACCOUNT
C FOR THE EPOCH CHANGE. ROTATION IS ABOUT THE TRUE OF DATE Z-AXIS.
C
      IF(NEEDROT2) THEN    ! DELEPOCH IS NON-ZERO
        IF(.NOT.OKROT2) THEN
          DNODE = EQVANG( CONST(65)*DELEPOCH )
          CALL ROT123(3,-DNODE,0,DUM,0,DUM,ROT2,0,KDUM)
          DTROT2 = DELEPOCH
          OKROT2 = .TRUE.
          IF(IBUG.NE.0) WRITE(LUBUG,8503) 'ROT2 COMPUTED.'
          END IF
        END IF
C
C
C GET ROT3, THE MATRIX THAT CONVERTS FROM THE TRUE OF DATE SYSTEM
C BACK TO THE GIVEN SYSTEM.
C
      IF(NEEDROT3) THEN    ! KCOORD = 1 OR 2 AND DELEPOCH NON-ZERO.
        IF(.NOT.OKROT3) THEN
          IF(KCOORD.EQ.1) THEN                     ! M50 -> TOD
            CALL M50TOD(TVEC+DELEPOCH,-1,ROT3)
          ELSE                                     ! MDT -> TOD
            CALL MDTTOD(TVEC+DELEPOCH,-1,ROT3)
            END IF
          TROT3 = TVEC + DELEPOCH
          OKROT3 = .TRUE.
          IF(IBUG.NE.0) WRITE(LUBUG,8503) 'ROT3 COMPUTED.'
          END IF
        END IF
C
C
C GET THE COMPOSITE MATRIX, ROTMTX = ROT3*ROT2*ROT1
C NEEDROTX=.FALSE. IS EQUIVALENT SO SAYING ROTX IS THE IDENTITY MATRIX.
C
      CALL MTXEQL(AIDENT,ROTMTX,3,3)
C
      IF(NEEDROT1)  THEN
        CALL MTXMUL33(1,ROT1,ROTMTX,ROTMTX)
        IF(IBUG.NE.0) WRITE(LUBUG,8504)
     *     'USED MATRIX ROT1.',((ROT1(I,J),J=1,3),I=1,3)
        END IF
C
      IF(NEEDROT2)  THEN
        CALL MTXMUL33(1,ROT2,ROTMTX,ROTMTX)
        IF(IBUG.NE.0) WRITE(LUBUG,8504)
     *     'USED MATRIX ROT2.',((ROT2(I,J),J=1,3),I=1,3)
        END IF
C
      IF(NEEDROT3)  THEN
        CALL MTXMUL33(1,ROT3,ROTMTX,ROTMTX)
        IF(IBUG.NE.0) WRITE(LUBUG,8504)
     *     'USED MATRIX ROT3.',((ROT3(I,J),J=1,3),I=1,3)
        END IF
C
C SAVE KCOORD FOR THE NEXT CALL WHEN THE COORDINATE SYSTEM FLAG WILL BE
C CHECKED AGAINST THE ONE ON THIS CALL.
C
      KCOORDL = KCOORD
C
 9999 CONTINUE
C
      IF(IBUG.NE.0) WRITE(LUBUG,8504)
     *       ' RETURNING ROTMTX TO CALLER.',((ROTMTX(I,J),J=1,3),I=1,3)
C
      RETURN
 8501 FORMAT(/,' ROTSLIP1 DEBUG. ENTRY VALUES ARE --'/,
     * '   KCOORD = ',I3,'  TVEC = ',G20.13,'  DELEPOCH(DAYS)=',G14.6)
 8502 FORMAT(' ROTSLIP1 DEBUG 8502.  NEEDROT=',3L2,'  OKROT=',3L2)
 8503 FORMAT(' ROTSLIP1 DEBUG. ',A)
 8504 FORMAT(' ROTSLIP1. ',A,'   CONTENTS=',
     *           3(/,T10,3G13.5))
      END
